package org.cy.spring.learning.webchat.websocket;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.socket.config.annotation.EnableWebSocket;
import org.springframework.web.socket.config.annotation.WebSocketConfigurer;
import org.springframework.web.socket.config.annotation.WebSocketHandlerRegistry;
import org.springframework.web.socket.server.support.HttpSessionHandshakeInterceptor;

/**
 * create date: 2019/9/26 19:06
 *
 * @author cao.yong
 */
@Configuration
@EnableWebSocket
public class WebSocketConfig implements WebSocketConfigurer {
    @Override
    public void registerWebSocketHandlers(WebSocketHandlerRegistry webSocketHandlerRegistry) {
        webSocketHandlerRegistry.addHandler(webChatHandler(), "/ws/chat")
                .addInterceptors(webChatHandshake()).setAllowedOrigins("*");
    }

    @Bean
    public WebChatHandler webChatHandler() {
        return new WebChatHandler();
    }

    @Bean
    public WebChatHandshake webChatHandshake() {
        return new WebChatHandshake();
    }
}
